How to Set Up a Real-Time Clock (RTC) with ESP32 and DS3231 Using NTP Time Synchronization

Introduction

Accurate timekeeping is crucial for electronics and IoT projects. A Real-Time Clock (RTC) is essential for maintaining precise time, especially during power outages. However, more than relying solely on the RTC module might be required for long-term projects due to drift over time. This is where Network Time Protocol (NTP) comes in. By synchronizing the RTC with an internet time server, you can ensure that your system stays perfectly in sync, regardless of how long it runs.

In this guide, we’ll show you how to set up a Real-Time Clock using ESP32 and an RTC DS3231 module, with the added benefit of NTP synchronization. Whether you’re new to electronics or an experienced developer, this tutorial will help you create a system that maintains precise and reliable time in all conditions.

Components Required

  • ESP32 (Main microcontroller)
  • DS3231 RTC Module (Accurate real-time clock)
  • 20×4 I2C LCD Display (To display time)
  • Jumper wires
  • Breadboard (optional)

Circuit Description

In this setup, the DS3231 RTC and the I2C LCD share the I2C communication protocol, using the same SDA (data line) and SCL (clock line) pins on the ESP32. This reduces the number of GPIO pins required and simplifies the wiring.

Checkout the article on How to interface RTC module with Arduino and ESP Board

How to Set Up a Real-Time Clock (RTC) with ESP32 and DS3231 Using NTP Time SynchronizationFigure 1: Real-Time Clock (RTC) with ESP32 and DS3231 Using NTP Time Synchronization

The ESP32 communicates with the RTC to retrieve the time and display it on the LCD. Additionally, the ESP32 synchronizes with an NTP server over Wi-Fi to keep the time accurate.

Circuit Connections

Below is the wiring guide for connecting the ESP32, DS3231, and I2C LCD.

Component Pin ESP32 Pin
DS3231 SDA GPIO 21
DS3231 SCL GPIO 22
DS3231 VCC VIN or 3.3V
DS3231 GND GND
I2C LCD SDA GPIO 21
I2C LCD SCL GPIO 22
I2C LCD VCC VIN
I2C LCD GND GND

Code Walkthrough

Libraries Used

  • Wire.h: Enables I2C communication.
  • LiquidCrystal_I2C.h: Controls the I2C LCD display.
  • WiFi.h: Manages the Wi-Fi connection.
  • NTPClient.h: Synchronizes time with an NTP server.
  • RTClib.h: Interacts with the DS3231 RTC.

setup()

In the setup() function, we initialize the RTC, ESP32 Wi-Fi, and NTP synchronization. This function also establishes communication with the 20×4 LCD.

loop()

The loop() checks the Wi-Fi status, updates the time via NTP, and retrieves the current time from the DS3231 RTC if the connection is successful. It then displays the time and a scrolling message on the LCD.

Displaying Time on the LCD

You can display the time in the format HH:MM:SS and scroll to a custom message in the top row.

Setting Up NTP Synchronization

Network Time Protocol (NTP) ensures that the time on your device stays accurate over the long term. In this section, you’ll learn how to set up NTP with the ESP32 and synchronize it with a reliable time server.

Conclusion

Setting up an RTC with ESP32 and NTP synchronization ensures that your project maintains accurate timekeeping, even during power outages. This setup is essential for many IoT and smart systems where precision timing is crucial.

By following this guide, you’ve learned how to:

  • Wire the DS3231 RTC and I2C LCD to the ESP32.
  • Set up NTP synchronization for accurate time.
  • Display the current time on an LCD screen.

wifi connecting

Figure 2: Wi-Fi Connecting

Time without net connected

Figure 3: Time when Wi-Fi is not connected

4 Thoughts to “How to Set Up a Real-Time Clock (RTC) with ESP32 and DS3231 Using NTP Time Synchronization”

  1. Henry Doan

    I have been retired a little over four years. It is great to see your logo again. I had followed your Engineering Projects Posts for a long time. The contents in this project are much clearer. Thank you for all your dedicated work that help a lot people like me.

    1. Thank you so much for your kind words and long-time support! It’s wonderful to hear that you’ve been following the Engineering Projects for so long, and I’m glad you found the content in this project clearer and helpful. Your appreciation means a lot, and it’s feedback like yours that keeps me motivated to continue creating valuable resources. Enjoy your retirement, and feel free to reach out anytime!

  2. Za

    Hi,
    Works fine!
    How can I modify the sketch, to start up with the time, date, etc. with battery in the RTC.
    With no internet (Wi-Fi) connection on switch on, there is no display.
    If the Wi-Fi fails when the unit is running, the time continues to move on.
    How can I display the time/ date from either the Wi-Fi connection or the RTC on startup?

  3. Zahir

    Hi,
    Works fine!
    How can I modify the sketch, to start up with the time, date, etc. with battery in the RTC.
    With no internet (Wi-Fi) connection on switch on, there is no display.
    If the Wi-Fi fails when the unit is running, the time continues to move on.
    How can I display the time/ date from either the Wi-Fi connection or the RTC on startup?

Leave a Comment